//
//  BaseViewController.swift
//  你的AI
//
//  Created by 丸子 on 2024/6/3.
//

import UIKit

class BaseViewController: UIViewController {
    /** 控制器第一次1s加载完成 */
    fileprivate(set) var loaded: Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.modalPresentationCapturesStatusBarAppearance = true
    }
    //    override var preferredStatusBarStyle: UIStatusBarStyle {
    //        return .lightContent
    //    }
    
    
    public func editNav(isCurrent: Bool){
        if isCurrent {
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : Theme.color.text_d1, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置导航栏标题文本属性
            self.navigationController?.navigationBar.tintColor = UIColor(hexString: "#FFFFFF") // 设置导航栏着色颜色
            if #available(iOS 13.0, *) { // 如果系统版本大于等于 iOS 13，则使用新的外观 API
                let appearancec = UINavigationBarAppearance() // 创建 UINavigationBarAppearance 对象
                appearancec.configureWithTransparentBackground() // 配置外观对象为透明背景
                appearancec.backgroundColor = .white
                appearancec.titleTextAttributes = [NSAttributedString.Key.foregroundColor : Theme.color.text_d1, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置外观对象标题文本属性
                self.navigationController?.navigationBar.standardAppearance = appearancec // 设置导航栏标准外观为外观对象
                self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance // 设置导航栏滚动边缘外观为标准外观
            }
            let btn = UIButton(type: UIButton.ButtonType.custom) // 创建自定义类型的按钮对象
            btn.setBackgroundImage(UIImage(named: "icon_paninting_back"), for: UIControl.State.normal) // 设置按钮正常状态下的背景图片
            btn.setBackgroundImage(UIImage(named: "icon_paninting_back"), for: UIControl.State.highlighted) // 设置按钮高亮状态下的背景图片
            btn.frame.size = btn.currentBackgroundImage?.size ?? CGSize.zero // 设置按钮尺寸为当前背景图片的尺寸
            btn.addTarget(self, action: #selector(clickReturn), for: UIControl.Event.touchUpInside) // 给按钮添加点击事件，点击时调用 clickReturn 方法
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn) // 将视图控制器的左侧导航项设置为自定义视图按钮对象
        }else {
            self.navigationController?.navigationBar.tintColor = UIColor(hexString: "#28272D") // 设置导航栏着色颜色
            if #available(iOS 13.0, *) { // 如果系统版本大于等于 iOS 13，则使用新的外观 API
                let appearancec = UINavigationBarAppearance() // 创建 UINavigationBarAppearance 对象
                appearancec.configureWithTransparentBackground() // 配置外观对象为透明背景
                appearancec.backgroundColor = Theme.color.main
                appearancec.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置外观对象标题文本属性
                self.navigationController?.navigationBar.standardAppearance = appearancec // 设置导航栏标准外观为外观对象
                self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance // 设置导航栏滚动边缘外观为标准外观
            }
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font: UIFont.pingFangSCMedium(ofSize: 16)] // 设置导航栏标题文本属性
        }
        
    }
    
    // 定义点击返回方法，用于在点击返回按钮时执行弹出视图控制器操作
    @objc func clickReturn() {
        self.navigationController?.popViewController(animated: true) // 弹出当前顶部的视图控制器
    }
}
